﻿// Handles notifications of keyboard key presses
define([   
    'Wjs/ui/elements/notifiers/keyupNotifier'
],
function () {
    'use strict';

    var keyupNotifier = Wjs.getType('keyupNotifier'),
        KeyUpHandler = Wjs.defineOnce({

            type: 'KeyUpHandler',

            internal: {

                construct: function (cfg, parent) {

                    this.listenOnKeyUp = cfg.listenOnKeyUp || this.listenOnKeyUp;
                    this.keyUpHandler = cfg.keyUpHandler || this.keyUpHandler;

                    if (this.listenOnKeyUp) { // If this view wants to get notified of a key pressed

                        keyupNotifier.subscribe(this);
                    }
                },

                listeners: {

                    // Called when the element is about to be destroyed
                    onDestroy: function () {

                        if (this.listenOnKeyUp) {

                            keyupNotifier.unSubscribe(this);
                        }

                        this.listenOnKeyUp = null;
                    }
                }
                
            }

            
        });

    return KeyUpHandler;
});